/*
*
* MDialog.java
* @author Rana
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.util.regex.*;

class MDialog extends JDialog implements ActionListener
{

    private String _ipaddress;
    private JLabel label1,label2,label3;
    private JTextField ip,serverPort,clientPort;
    private JButton ok;
    private JButton cancel;
    private Container container;

    public MDialog(JFrame jframe)
    {
        super(jframe, "Enter IP", true);
        _ipaddress = null;
        initDialogBox(jframe);
    }

    private void initDialogBox(JFrame jframe)
    {
		setDefaultLookAndFeelDecorated(true);
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        container = getContentPane();
        container.setLayout(null);
        label1 = new JLabel("Enter IP Address:");
		label1.setBounds(10, 10, 115, 20);
        ip = new JTextField();
		ip.setBounds(115, 10, 100, 20);

		label2 = new JLabel("Server Port:");
		label2.setBounds(10, 30, 115, 20);
        serverPort = new JTextField("4876");
		serverPort.setBounds(115, 30, 60, 20);
//serverPort.setEnabled(false);
		label3 = new JLabel("Client Port:");
		label3.setBounds(10, 50, 115, 20);
        clientPort = new JTextField("4876");
		clientPort.setBounds(115, 50, 60, 20);
//clientPort.setEnabled(false);
        ok = new JButton("OK");
        cancel = new JButton("Cancel");
		ok.setBounds(30, 90, 70, 20);
        cancel.setBounds(110, 90, 80, 20);

        container.add(label1);
		container.add(label2);
		container.add(serverPort);
		container.add(label3);
		container.add(clientPort);
        container.add(ip);
        container.add(ok);
        container.add(cancel);
		
        ok.addActionListener(this);
        cancel.addActionListener(this);

        //setResizable(false);
		setSize(240, 180);
        setLocationRelativeTo(jframe);
		//pack();
        setVisible(true);
    }

    public void actionPerformed(ActionEvent actionevent)
    {
        if(actionevent.getSource() == ok || actionevent.getSource() == ip)
        {
            _ipaddress = ip.getText();
            if(_ipaddress.length() == 0)
            {
		 	  
                JOptionPane.showMessageDialog(this, "Please enter IP", "Error", 2);
                return;
            }
			else
			{
				Pattern pattern;
				Matcher matcher;
				try
				{
					pattern = Pattern.compile("^([0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3})$");
					matcher = pattern.matcher(_ipaddress);
					if (_ipaddress.matches("^[\\d]{1,3}\\.[\\d]{1,3}\\.[\\d]{1,3}\\.[\\d]{1,3}$"))
					{
						 //JOptionPane.showMessageDialog(this, "Please enter Valid IP", "Error", 2);
		                 //return;
					}
				}
				catch (Exception e)	{}
			}
        } else if(actionevent.getSource() == cancel)
		{
			System.exit(0);
		}
        setVisible(false);
    }

    protected String getIP()
    {
        return _ipaddress;
    }

	protected int getServerPort()
    {
        return Integer.parseInt(serverPort.getText());
    }

	protected int getClientPort()
    {
        return Integer.parseInt(clientPort.getText());
    }
}
